﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.BD.VideoMatrixConfigTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadVideoMatrixConfig();
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            VideoMatrixWindow videoMatrixWindow = new VideoMatrixWindow();
            videoMatrixWindow.Closed += videoMatrixWindow_Closed;
            videoMatrixWindow.Show();
        }

        void videoMatrixWindow_Closed(object sender, EventArgs e)
        {
            LoadVideoMatrixConfig();
        }

        private void _ctlEdit_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlDataGridSolution.SelectedItem != null)
            {
                Datas.Solution solution = _ctlDataGridSolution.SelectedItem as Datas.Solution;
                VideoMatrixWindow videoMatrixWindow = new VideoMatrixWindow(solution);
                videoMatrixWindow.Closed += videoMatrixWindow_Closed;
                videoMatrixWindow.Show();
            }
            else
            {
                MessageBox.Show("请选择要编辑的信息！");
            }
        }

        private void _ctlDelete_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlDataGridSolution.SelectedItem != null)
            {
                Datas.Solution solution = _ctlDataGridSolution.SelectedItem as Datas.Solution;
               if(XSolutionConfigUntil.DeleteSolution(GetSolutionFilePath(),solution))
                {
                    LoadVideoMatrixConfig();
                    MessageBox.Show("删除信息成功!");
                }
                else
                {
                    MessageBox.Show("删除信息失败!");
                }
            }
        }

        private void _ctlSave_Click(object sender, RoutedEventArgs e)
        {

        }

        private void _ctlCanle_Click(object sender, RoutedEventArgs e)
        {

        }


        private void LoadVideoMatrixConfig()
        {
            _ctlDataGridSolution.ItemsSource = null;
            List<Datas.Solution> ListSolution = XSolutionConfigUntil.GetListSolution(GetSolutionFilePath());
            if (ListSolution != null)
            {
                _ctlDataGridSolution.ItemsSource = ListSolution;
            }
         
        }

        /// 获取solution.xml文件的地址
        /// </summary>
        /// <returns></returns>
        private static string GetSolutionFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "solution.xml");
        }
    }
}
